Dashboard Temp Share Shortlinks Frames API

HTMLify

Search a 2D Matrix II OR Search in a row-column sorted Matrix.cpp
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int n = matrix.size();
        int m = matrix[0].size();
        int i =0; 
        int j = m - 1;

        while(i<n && j>=0){
            if(matrix[i][j] == target){
                return true;
            }
            else if (matrix[i][j] > target){
                j--;
            }
            else{
                i++;
            }
        }
        return false;
    }
};